package cn.mandifal.seller.controller;
import entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.mandifal.utils.FastDFSClient;
/**
 * 文件上传控制层
 * @author shun
 */
@RestController
@RequestMapping("fileUploadController")
public class FileUploadController {
    /**
     * 把引入的配置文件进行注入,设置服务器 ip
     */
    @Value("${FILE_SERVER_URL}")
    private String fileServerUrl;

    @RequestMapping("upload")
    public Result upload(MultipartFile multipartFile){
        String originalFilename = multipartFile.getOriginalFilename();
        //得到扩展名,通过最后.的索引去截取字符串，但包括这个.所以+1
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        try {
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
            String fileId = fastDFSClient.uploadFile(multipartFile.getBytes(), extName);
            //图片完整地址
            String pictureUrl= fileServerUrl +fileId;
            return Result.success(pictureUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
}
